package com.jinly.ryder.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jinly.ryder.common.utils.DESUtil;
import com.jinly.ryder.manage.config.SecretProperties;
import com.jinly.ryder.manage.dao.InstanceSecretMapper;
import com.jinly.ryder.manage.dao.entity.InstanceSecretDO;
import com.jinly.ryder.manage.service.InstanceSecretService;
import com.jinly.ryder.manage.service.mapping.InstanceSecretMapping;
import com.jinly.ryder.manage.service.model.InstanceSecret;
import com.jinly.ryder.manage.service.model.InstanceType;
import com.jinly.ryder.manage.service.model.SecretMode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class InstanceSecretServiceImpl implements InstanceSecretService {

    private final InstanceSecretMapping instanceSecretMapping;
    private final InstanceSecretMapper instanceSecretMapper;
    private final SecretProperties secretProperties;

    @Override
    public InstanceSecret getPlainSecret(InstanceType instanceType, Integer instanceId) {
        InstanceSecret instanceSecret = getSecret(instanceType, instanceId);
        decrypt(instanceSecret);
        return instanceSecret;
    }

    @Override
    public InstanceSecret getSecret(InstanceType instanceType, Integer instanceId) {
        Wrapper<InstanceSecretDO> queryWrapper = new QueryWrapper<InstanceSecretDO>().lambda()
                .eq(InstanceSecretDO::getInstanceType, instanceType)
                .eq(InstanceSecretDO::getInstanceId, instanceId);
        return instanceSecretMapping.toModel(instanceSecretMapper.selectOne(queryWrapper));
    }

    private void encrypt(InstanceSecret instanceSecret) {
        String cipherText = null;
        if (instanceSecret == null || instanceSecret.getMode() == null) return;
        switch (instanceSecret.getMode()) {
            case DES:
                cipherText = DESUtil.encrypt(instanceSecret.getPlainText(), secretProperties.getSecretKey());
                break;
            default:
                cipherText = instanceSecret.getPlainText();
        }
        instanceSecret.setCipherText(cipherText);
    }

    private void decrypt(InstanceSecret instanceSecret) {
        String plainText = null;
        if (instanceSecret == null || instanceSecret.getMode() == null) return;
        switch (instanceSecret.getMode()) {
            case DES:
                plainText = DESUtil.decrypt(instanceSecret.getCipherText(), secretProperties.getSecretKey());
                break;
            default:
                plainText = instanceSecret.getCipherText();
        }
        instanceSecret.setPlainText(plainText);
    }


    @Override
    public void saveSecret(InstanceSecret instanceSecret) {
        instanceSecret.setMode(SecretMode.DES);
        encrypt(instanceSecret);
        InstanceSecret secret = getSecret(instanceSecret.getInstanceType(), instanceSecret.getInstanceId());
        if (secret == null) {
            instanceSecretMapper.insert(instanceSecretMapping.toEntity(instanceSecret));
        } else {
            instanceSecret.setId(secret.getId());
            instanceSecretMapper.updateById(instanceSecretMapping.toEntity(instanceSecret));
        }
    }
}
